package org.marsdonne.commons.oss;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;

import javax.annotation.Resource;
import java.io.InputStream;

public class ObjectStorageServImpl implements ObjectStorable {
    @Resource
    private AmazonS3Client amazonS3Client;

    @Override
    public UploadResult upload(final String space, final String category, final String name, long contentLength, String contentType, final InputStream inputStream) {
        String key = category + "/" + name;
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(contentLength);
        metadata.setContentType(contentType);

        return new UploadResult(
                this.amazonS3Client.putObject(space, key, inputStream, metadata).getContentMd5(),
                this.amazonS3Client.getUrl(space, key).toString()
        );
    }
}
